package com.ufo.store.management.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.ufo.store.management.ui.fragment.MemberListFragment;

import java.util.List;

/**
 *  作者：taoli on 2017/2/28 0028 11:28
 *  
 */
public class MemberViewPagerAdapter extends FragmentPagerAdapter {
    private FragmentManager mFragmentManager;
    private List<String> listPoint;
    private MemberListFragment.MemberListGetProcessListener listener;
    private int ListStyle;
    private int SearchSortMode;

    public MemberViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        mFragmentManager = fragmentManager;
    }

    @Override
    public int getCount() {
        return listPoint.size();
    }

    @Override
    public Fragment getItem(int position) {
        MemberListFragment fragment = new MemberListFragment();
        fragment.setPosition(position);
        fragment.setMemberPointList(listPoint);
        fragment.setMemberListStyle(ListStyle);
        fragment.setMemberListGetProcessListener(listener);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    public void setStyle(int s){
        ListStyle = s;
        List<Fragment> memberFragments = mFragmentManager.getFragments();
        if (memberFragments != null && memberFragments.size() != 0) {
            for (int i = 0; i < memberFragments.size(); i++) {
                MemberListFragment memberFragment = (MemberListFragment) memberFragments.get(i);
                if (memberFragment != null  && memberFragment.getUserVisibleHint()) {
                    memberFragment.setStyle(s);
                }
            }
        }
    }

    public void setTitleIsHided(boolean is){
        List<Fragment> memberFragments = mFragmentManager.getFragments();
        if (memberFragments != null && memberFragments.size() != 0) {
            for (int i = 0; i < memberFragments.size(); i++) {
                MemberListFragment memberFragment = (MemberListFragment) memberFragments.get(i);
                if (memberFragment != null  && memberFragment.getUserVisibleHint()) {
                    memberFragment.setTitleIsHided(is);
                }
            }
        }
    }

    public void setSearchSortMode(int m){
        SearchSortMode = m;
        List<Fragment> memberFragments = mFragmentManager.getFragments();
        if (memberFragments != null && memberFragments.size() != 0) {
            for (int i = 0; i < memberFragments.size(); i++) {
                MemberListFragment memberFragment = (MemberListFragment) memberFragments.get(i);
                if (memberFragment != null && memberFragment.getUserVisibleHint()) {
                    memberFragment.setMemberSearchSortMode(m);
                }
            }
        }
    }

    public void setMemberListStyle(int s){
        ListStyle = s;
    }

    public void setMemberPointList(List<String> list) {
        this.listPoint = list;
    }

    public void setMemberListGetProcessListener(MemberListFragment.MemberListGetProcessListener lis) {
        this.listener = lis;
    }

    public boolean isFragmentVisible(int position) {
        boolean isV = false;
        List<Fragment> memberFragments = mFragmentManager.getFragments();
        if (memberFragments != null && memberFragments.size() != 0) {
            for (int i = 0; i < memberFragments.size(); i++) {
                MemberListFragment memberFragment = (MemberListFragment) memberFragments.get(i);
                if (memberFragment != null && memberFragment.getUserVisibleHint()) {
                    if (i == position)
                        isV = true;
                }
            }
        }
        return isV;
    }
}

